function [Q obs] = Ackerberg_gmm(beta,lambda,data)
  % INPUT: beta:   labor & capital coefficient. 
  %
  % OUTPUT: GMM criterion function + no of observations used.
  % Note: We are estimating a value added PF here.

  global omega_coeffs seomega_coeffs;
  omega_coeffs = []; seomega_coeffs = [];

  % Omega is productivity
  omega = lambda - beta(1)*data.lnK;  

  % Lag of omega
  omega_lag = NaN(data.n,1);
  M = [data.firmid data.year];
  for i=2:length(data.firmid)                              
    if (M(i-1,1)==M(i,1) & M(i-1,2)==M(i,2)-1) omega_lag(i) = omega(i-1); end
  end
   
  % Find an estimate of E(omega_t | omega_t-1) 
  X = [ones(data.n,1) omega_lag data.Ex];

  [b ci] = regress(omega,X);     
  alpha = .05;
  t = tinv(1-alpha/2,data.n-length(b));
  seomega_coeffs = ((ci(:,2)-ci(:,1))./(2*t))';   
  omega_coeffs = b';
  
  % The innovation xi = omega - E(omega) is then
  xi = omega - X*b;
  
  % Calculate objective function for given coefficient estimates
  % Note: We only include observations from the second period the firm is
  % observed (both for lagged and non-lagged variables). This happens automatically
  % because xi_t is missing if the firms is not operating in (t-1).

  mom(1,1) = nanmean(xi.*data.lnK);         % OP & Ackerberg & LP
  mom(2,1) = nanmean(xi.*data.LlnInp);      % LP
  mom(3,1) = nanmean(xi.*data.LlnK);        % Generate overidentifying restrictions, get extra identifying power  
  
  
  Q = mom'*mom;     % Identity weighting matrix

   % No of obs
  obs = sum(isnan(xi)==0);
  
end
